package com.litign.personcontrol.controller;

import com.litign.personcontrol.pojo.Emp;
import com.litign.personcontrol.pojo.Result;
import com.litign.personcontrol.service.EmpService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
public class LoginController {
    private static final Logger log = LoggerFactory.getLogger(EmpController.class);

    @Autowired
    private EmpService empService;

    @PostMapping("/login")
    public Result login(@RequestBody Emp emp) {
        log.info("员工登录:{}",emp);
        Emp e = empService.login(emp);

        //登录成功，生成，下发令牌
        if(e != null) {
            Map<String, Object> claims = new HashMap<>();
            claims.put("id", e.getId());
            claims.put("name", e.getName());
            claims.put("username", e.getUsername());

//            String jwt = JwtUtils.generateJwt(claims);
            return Result.success(claims);
//            return Result.success(jwt);
        }

        return Result.error("用户名或密码错误");
    }

}
